function atoA(srt) {
	var reg = /[（]/g,
		reg2 = /[）]/g;
	return srt.replace(reg, "(").replace(reg2, ")");
}

// toast
function toast(title, duration = 1500, isSuccess) {
	isSuccess
		?
		uni.showToast({
			title: title,
			icon: "success",
			duration: duration,
		}) :
		uni.showToast({
			title: title,
			icon: "none",
			duration: duration,
		});
}

toast.success = (title, duration = 1500) => {
	uni.showToast({
		title: title,
		icon: "success",
		duration: duration,
	})
}
toast.fail = (title, duration = 1500) => {
	uni.showToast({
		title: title,
		icon: "error",
		duration: duration,
	})
}

module.exports = {
	toast,
	atoA,
	//js显示早上好下午好晚上好
	getTimeState() {
		// 获取当前时间
		let timeNow = new Date();
		// 获取当前小时
		let hours = timeNow.getHours();
		// 设置默认文字
		let text = ``;
		// 判断当前时间段
		if (hours >= 0 && hours <= 10) {
			text = `早上好`;
		} else if (hours > 10 && hours <= 14) {
			text = `中午好`;
		} else if (hours > 14 && hours <= 18) {
			text = `下午好`;
		} else if (hours > 18 && hours <= 24) {
			text = `晚上好`;
		}
		// console.log(`hours >>>>>`, hours);
		// console.log(`text >>>>`, text);
		// 返回当前时间段对应的状态
		return text;
	},
	//判断string、object是否为空
	isEmpty(varvar) {
		if (varvar == undefined || varvar == null || !varvar) {
			return true;
		}
		if (typeof varvar == "string") {
			if (varvar.length == 0) {
				return true;
			} else {
				return false;
			}
		} else if (typeof varvar == "object") {
			for (var key in varvar) {
				return false;
			}
			return true;
		}
	},
	// 切换时间格式 165222222555转 为2021年7月24日 10:19
	needDate(time) {
		// console.log("time", this.formatDateV2(+time))
		const date = new Date(Date.parse(this.formatDateV2(+time).replace(/-/g, "/")));
		
		var year = date.getFullYear() + "年";
		var month = date.getMonth() + 1 + "月";
		var day = date.getDate() + "日";
		var hh = date.getHours() + ":";
		var mm = date.getMinutes();
		var ss = date.getSeconds();
		mm = mm.toString().length < 2 ? "0" + mm : mm;
		return year + month + day + " " + hh + mm;
	},
	
	// 标准时间转多少分钟之前
	//2021-04-16 15:53:07
	putDate(time, type) {
		if (!time) {
			return "--";
		}
		const date = Date.parse(time.replace(/-/g, "/"));
		// 时间字符串转时间戳
		var timestamp = new Date(date).getTime();
		
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var month = day * 30;
		var year = day * 365;
		var now = new Date().getTime();
		var diffValue = now - timestamp;
		var result;
		if (diffValue < 0) {
			return;
		}
		var yearC = diffValue / year;
		var monthC = diffValue / month;
		var weekC = diffValue / (7 * day);
		var dayC = diffValue / day;
		var hourC = diffValue / hour;
		var minC = diffValue / minute;
		if (yearC >= 1) {
			result = "" + parseInt(yearC) + "年前";
		} else if (monthC >= 1) {
			result = "" + parseInt(monthC) + "月前";
		} else if (weekC >= 1) {
			result = "" + parseInt(weekC) + "周前";
		} else if (dayC >= 1) {
			result = "" + parseInt(dayC) + "天前";
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时前";
		} else if (minC >= 5) {
			result = "" + parseInt(minC) + "分钟前";
		} else {
			switch (type) {
				case "state":
					result = "刚刚活跃";
					break;
				default:
					result = "刚刚";
			}
		}
		return result;
	},
	// 标准时间转时间挫 //2021-08-12 10:2 to 1627698586
	dateToTimestamp(time) {
		const date = new Date(Date.parse(time.replace(/-/g, "/")));
		return Date.parse(date);
	},
	//获取当前时间 格式：2013-01-01  00:00:00
	currentTime() {
		var now = new Date();
		
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var day = now.getDate();
		
		var hh = now.getHours();
		var mm = now.getMinutes();
		var ss = now.getSeconds();
		var clock = year + "-";
		
		if (month < 10) clock += "0";
		
		clock += month + "-";
		
		if (day < 10) clock += "0";
		
		clock += day + " ";
		
		if (hh < 10) clock += "0";
		clock += hh + ":";
		if (mm < 10) clock += "0";
		clock += mm + ":";
		if (ss < 10) clock += "0";
		clock += ss;
		return clock;
	},
	//获取当前年月日 格式：2013-01-01
	currentYMD() {
		var now = new Date();
		
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var day = now.getDate();
		
		var hh = now.getHours();
		var mm = now.getMinutes();
		var ss = now.getSeconds();
		var clock = year + "-";
		
		if (month < 10) clock += "0";
		
		clock += month + "-";
		
		if (day < 10) clock += "0";
		
		clock += day
		return clock;
	},
	//时间戳转换方法    date:时间戳数字 转 2013-01-01 00:00:00
	formatDate(date, isNoSeconds = false) {
		console.log("date", date)
		var date = new Date(date);
		var YY = date.getFullYear() + "-";
		var MM = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
		var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
		var hh = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
		var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
		var ss = "";
		if (!isNoSeconds) ss = ":" + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
		else ss = "";
		
		return YY + MM + DD + " " + hh + mm + ss;
	},
	formatDateV2(date, type = 'YYYY-MM-DD hh:mm:ss') {
		var date = isNaN(date) ? new Date(date.replace(/-/g, '/')) : new Date(date);
		var YY = date.getFullYear() + "-";
		var MM = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
		var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
		var hh = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
		var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
		var ss = ":" + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
		let str = ''
		// console.log("hh", hh)
		// console.log("mm", mm)
		if (type.indexOf('Y') >= 0) {
			str = str + YY
		}
		if (type.indexOf('M') >= 0) {
			str = str + MM
		}
		if (type.indexOf('D') >= 0) {
			str = str + DD
		}
		if (type.indexOf('h') >= 0) {
			if (type.indexOf('D') >= 0) {
				str = str + ' '
			}
			str = str + hh
		}
		if (type.indexOf('m') >= 0) {
			str = str + mm
		}
		if (type.indexOf('s') >= 0) {
			str = str + ss
		}
		// console.log("str", str)
		return str;
	},
	// 对象转string
	toStr(obj) {
		return encodeURIComponent(JSON.stringify(obj));
	},
	// string转对象
	toObj(str) {
		return JSON.parse(decodeURIComponent(str));
	},
	removeProperties(obj1, obj2) {
		// 获取两个对象所有的属性
		let keys1 = Object.keys(obj1);
		let keys2 = Object.keys(obj2);
		
		// 合并两个对象的属性
		let allKeys = [...keys1, ...keys2];
		
		// 过滤掉在 obj2 中出现过的属性
		let result = allKeys.filter(key => !keys2.includes(key));
		
		// 返回过滤后的对象
		return result.reduce((resultObj, key) => {
			resultObj[key] = obj1[key];
			return resultObj;
		}, {});
	},
	
	getRealObj(obj) {
		Object.keys(obj).reduce((result, key) => {
			if (obj[key] !== null) {
				result[key] = obj[key]
			}
			return result
		}, {})
	},
	//一键复制
	copy(copyObject, text) {
		uni.setClipboardData({
			data: copyObject,
			success: (data) => {
				uni.showToast({
					title: text || "复制成功",
					icon: "success",
					mask: false,
				});
			},
		});
	},
	//拨打电话
	openPhone(number) {
		if (!number) {
			return;
		}
		uni.makePhoneCall({
			phoneNumber: number,
		});
	},
	/**
	 * @description: 预览图片
	 * @param {Array} imgList
	 * @return {*}
	 */
	lookImg(imgList, current) {
		uni.previewImage({
			current: current || 0,
			urls: imgList,
		});
	},
	// 获取当前页面options参数
	getOptions() {
		// 获取页面参数
		const vm = getCurrentPages()[getCurrentPages().length - 1]; //当前页面实例
		return vm.options;
	},
	// 获取上一页页面实例
	beforeVm(page = 1) {
		return getCurrentPages()[getCurrentPages().length - (page + 1)].$vm; //当前页面实例
	},
	// 获取当前页面实例
	getVm() {
		return getCurrentPages()[getCurrentPages().length - 1].$vm; //当前页面实例
	},
	guid() {
		var d = new Date().getTime();
		var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
			var r = (d + Math.random() * 16) % 16 | 0;
			d = Math.floor(d / 16);
			return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
		});
		
		return uuid;
	},
	// 比较版本号
	compareVersion(v2) {
		let {SDKVersion: v1} = uni.getSystemInfoSync();
		v1 = v1.split(".");
		v2 = v2.split(".");
		const len = Math.max(v1.length, v2.length);
		
		while (v1.length < len) {
			v1.push("0");
		}
		while (v2.length < len) {
			v2.push("0");
		}
		
		for (let i = 0; i < len; i++) {
			const num1 = parseInt(v1[i]);
			const num2 = parseInt(v2[i]);
			
			if (num1 > num2) {
				return 1;
			} else if (num1 < num2) {
				return -1;
			}
		}
		return 0;
	},
	/**
	 * 计算两个日期之间的天数
	 * @param dateString1  开始日期 yyyy-MM-dd
	 * @param dateString2  结束日期 yyyy-MM-dd
	 * @param type   days 纯转换天数 hours小时
	 * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期，返回0
	 */
	getDays(dateString1, dateString2, type) {
		const startDate = new Date(dateString1.replace(/-/g, "/").replace(/T/, " ")).getTime()
		const endDate = new Date(dateString2.replace(/-/g, "/").replace(/T/, " ")).getTime()
		
		if (startDate >= endDate) {
			return 0;
		}
		const days = endDate - startDate
		//计算出相差天数
		const day = Math.floor(days / (24 * 3600 * 1000))
		//计算出小时数
		const leave1 = days % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
		const hours = Math.floor(leave1 / (3600 * 1000))
		//计算相差分钟数
		const leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
		const minutes = Math.floor(leave2 / (60 * 1000))
		//计算相差秒数
		const leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
		const seconds = Math.round(leave3 / 1000)
		// alert(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
		if (type === 'days') {
			return (days / (24 * 3600 * 1000)).toFixed(1)
		}
		if (type === 'hours') {
			return (days / (24 * 3600 * 1000) * 24).toFixed(1)
		}
		if (type === 'minutes') {
			return (days / (24 * 3600 * 1000) / 24 * 3600).toFixed(1)
		}
		
		//全部输出
		let str = ''
		if (day) {
			str += day + "天"
		}
		if (hours) {
			str += hours + "时"
		}
		if (minutes) {
			str += minutes + "分"
		}
		
		return str
	},
	isOvertime(time) { //是否到期
		return (+new Date() > this.dateToTimestamp(time))
	},
	// JS 获取 URL参数
	getParam(variable, url) {
		// var query = window.location.search.substring(1)
		var query = url && url.split("?")[1] || window.location.hash.split("?")[1]
		var vars = query.split('&')
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split('=')
			if (pair[0] == variable) {
				return pair[1]
			}
		}
		return false
	},
};
